<?php
  class news_model extends CI_Model{
	
	function __construct()
	{
		parent::__construct();
	}
      // Category                 
      /*function getAllCat($num,$offset){
          $this->db->where('App',0);
          $this->db->where('ParentID',0);
          $query = $this->db->get('contentcat');
          return $query->result();
      }
      function getNumCat(){
          $this->db->where('App',0);
          $this->db->where('ParentID',0);
          $query = $this->db->get('contentcat');
          return $query->num_rows();
      }
      
      function getItemCat(){
          $this->db->where('App',0);
          $catid = $this->uri->segment(3);
          $this->db->where('CatID',$catid);
          $query = $this->db->get('contentcat');
          return $query->row();
      }
      
      function get_Parent($CatID){
          $this->db->where('App',0);
          $this->db->where('ParentID',$CatID);
          return $this->db->get('contentcat')->result();
      }
      
      function saveUpdateCat(){
          $id = (int)$this->uri->segment(3);
          $CatName = $this->input->post('CatName');
          $Alias  = $this->input->post('Alias');
          $ParentID  = $this->input->post('ParentID');
          $IsActive = $this->input->post('IsActive');
          $data = array(
            'CatName' => $CatName,
            'Alias' => strtolower(stringchange($CatName)),
            'ParentID' => $ParentID,
            'App' => 0,
            'IsActive' => $IsActive
          );
          if($id != 0){
              $this->db->where('CatID',$id);
              if($this->db->update('contentcat',$data)){
                  return true;
              }else{
                  return false;
              }
          }else{

              if($this->db->insert('contentcat',$data)){
                  return true;
              }else{
                  return false;
              }
          }          
      }
      
      function delcat($id){
          $this->db->where('App',0);
          $this->db->where('CatID',$id);
          if($this->db->delete('contentcat')){
              return true;
          }else{
              return false;
          }
      }*/
      
      // Begin List News
      function getAllNews($num,$offset){ 
          $this->db->where('bl_active',1);     
          $this->db->order_by('lb_content','DESC');
          $query = $this->db->get('news',$num,$offset);
          return $query->result();
      }
      function getNumNews(){      
          $this->db->where('bl_active',1);
          $query = $this->db->get('news');
          return $query->num_rows();
      }
      
/*      function getListCat(){
          return $this->db->get('contentcat')->result();
      }   
*/      //Save news
      function savenews(){
          $id = (int)$this->uri->segment(3);
          $data = array(
            'news_id' => (int)$this->input->post('CatID'),
            'lb_title' => (string)$this->input->post('ContentTitle'),
            'lb_alias' => strtolower(stringchange($this->input->post('ContentTitle'))),
            'lb_image' => (string)$this->input->post('ContentImg'),
            'lb_summary' => (string)$this->input->post('ContentSmall'),
            'lb_content' => (string)$this->input->post('ContentBody'),
            'bl_active' => (int)$this->input->post('IsActive'),
            //'App' => 0,
            'dt_create' => time()
          );
          if($id!=0){
              $this->db->where('news_id',$id);
              if($this->db->update('news',$data)){
                  return true;
              }else{
                  return false;
              }
          }else{
              if($this->db->insert('news',$data)){
                  return true;
              }else{
                  return false;
              }
          }
      }
      
      function delnews($id){
          $this->db->select('ContentID, ContentImg');
          $this->db->where('App',0);
          $this->db->where('ContentID',$id);
          $check = $this->db->get('content')->row();
          if($check){
              if(file_exists('../uploads/news/'.$check->ContentImg)){
                  unlink('../uploads/news/'.$check->ContentImg);
              }
              $this->db->where('ContentID',$id);
              if($this->db->delete('content')){
                  return true;
              }else{
                  return false;
              }
          }else{
              return false;
          }
      }
      
      //Update News
      function getItemNews(){
          $id = (int)$this->uri->segment(3);
          $this->db->where('App',0);
          $this->db->where('ContentID',$id);
          $query = $this->db->get('content');
          return $query->row();
      }
  }
?>
